<?php

/**
 * Created by PhpStorm.
 * User: lcw-home
 * Date: 2015/11/15
 * Time: 11:23
 */
namespace System\Model;

use Common\AdminModel;
use Common\Lib\Auth;

class UserLoginModel extends AdminModel
{
    public function validateUsername($username)
    {
        return true;
        $pattern = '/[0-9a-zA-Z\-_\.]*/i';
        $check = preg_match($username, $pattern);
        return filter_var($username, FILTER_VALIDATE_EMAIL) || $check;
    }

    public function login($username, $password)
    {
        if ($this->validateUsername($username) && 7 < strlen($password)) {
            $where['username'] = $username;
            $user = $this->where($where)->find();
            if (!$user) {
                $this->error = L('LOGIN_FAILED');
                return 404;
            }
            if (time() < $user['need_captcha']) {
                $verify = new \Think\Verify();
                if (!$verify->check(I('captcha'))) {
                    $this->error = L('CAPTCHA_ERROR');
                    return 412;
                }
            }
            if ($user['password'] !== superHash($password)) {

                $data['need_captcha'] = time() + 60 * 5;
                $data['id'] = $user['id'];
                $this->data($data)->save();
                $this->error = L('LOGIN_FAILED');
                return 400;
            }
            if ($user['status'] == 0) {
                $this->error = L('USER_DISABLED');
                return 403;
            }
            /* 登录用户 */
            $this->autoLogin($user['uid']);
            return 200;
        } else {
            $this->error = L('ILLEGAL_INPUT');
            return 406;
        }
    }
    /**
     * 注销当前用户
     * @return void
     */
    public function logout()
    {
        session('user_auth', null);
        session('user_auth_sign', null);
    }

    /**
     * 自动登录用户
     * @param  integer $user 用户信息数组
     */
    private function autoLogin($uid)
    {
        tag('before_login',$uid);
        $user = D('User')->find($uid);
        if ($user) {

            /* 更新登录信息 */
            $data = array(
                'uid' => $uid,
                'login' => array('exp', '`login`+1'),
                'last_login_time' => NOW_TIME,
                'last_login_ip' => get_client_ip(1),
                'need_captcha' => 0,
                'session_id' => session('id')
            );
            $this->save($data);
            $rules = Auth::getByUserFull();
            $auth = array(
                'uid' => $uid,
                'gid' => $user['s_gid'],
                'model'=>$user['model'],
                'nickname' => $user['nickname'],
                'rules' => $rules
            );
            session('user_auth', $auth);
            session('user_auth_sign', data_auth_sign($auth));
        } else {
            //TODO:error collect
        }
        tag('after_login',$user);
    }

    /**
     * 获得用户基本信息对照数组
     * @return array
     */
    public function getCache()
    {
        $data = $this->field('id,nickname,gid')->select();
        $cache = array();
        foreach ($data as $d) {
            $cache[$d['id']] = $d;
        }
        return $cache;
    }
}